package com.mingqijia.gassafety.shared.utils;

import com.gap.basic.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: gassafety
 * @description: 比较对象中属性值的不同
 * @author: Mr.Wang
 * @create: 2023-02-10 11:08
 **/
@Component
@Slf4j
public class CompareUtil<T> {

    public  Map<String, String> compare(Field[] fs, T o1, T o2){
        Map<String, String> nowobj = new HashMap<>();
        for (Field f : fs) {
            f.setAccessible(true);
            Object v1 = null;
            Object v2 = null;
            try {
                v1 = f.get(o1);
                v2 = f.get(o2);
            } catch (IllegalArgumentException | IllegalAccessException e) {
                throw new BizException("比较异常");
            }
            if (!equals(v1, v2)) {
                String s = StringUtils.isEmpty(v2)?"- ":v2.toString();

                log.info("测试用日志:字段名:{},值:{}",f.getName(),s);
                nowobj.put(f.getName(), s);
            }
        }
        return nowobj;
    }
    public static boolean equals(Object obj1, Object obj2) {

        if (obj1 == obj2) {
            return true;
        }
        if (obj1 == null || obj2 == null) {
            return false;
        }
        return obj1.equals(obj2);
    }
}
